﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using AEM.Data;
using Actions = AEM.Data.BLL.Actions;

namespace AEM
{
    /// <summary>
    /// EscalatorMonitoring.xaml 的交互逻辑
    /// </summary>
    public partial class EscalatorMonitoring : Window
    {
        public event EventHandler<EventArgs> HideEventHandler;
        private string liftnum = "";
        private Data.Model.LiftInfo liftinfo = null;
        public EscalatorMonitoring()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(EscalatorMonitoring_MouseLeftButtonDown);
            this.btn_close.Click += new RoutedEventHandler(btn_close_Click); ;
            this.Loaded += new RoutedEventHandler(EscalatorMonitoring_Loaded);
        }

        void btn_stop_Click(object sender, RoutedEventArgs e)
        {
            if (!Common.PermissionManager.hasPower((int)Data.Enum.ControlPoints.StopRuning))
            {
                MessageBox.Show((string)TryFindResource("PermissionDenied"));
                return;
            }
            if (null != Actions.SerialPorts.Regulate && null != liftinfo)
                Actions.SerialPorts.Regulate.BeginInvoke(true,
                    liftinfo.LiftNum, liftinfo.LiftCOM, byte.Parse("A", NumberStyles.HexNumber), 0, new AsyncCallback(Actions.SerialPorts.RegulateCallback), null);
        }
        void btn_descending_Click(object sender, RoutedEventArgs e)
        {
            if (!Common.PermissionManager.hasPower((int)Data.Enum.ControlPoints.DescendingStart))
            {
                MessageBox.Show((string)TryFindResource("PermissionDenied"));
                return;
            }
            if (null != Actions.SerialPorts.Regulate && null != liftinfo)
                Actions.SerialPorts.Regulate.BeginInvoke(true,
                    liftinfo.LiftNum, liftinfo.LiftCOM, byte.Parse("9", NumberStyles.HexNumber), 0, new AsyncCallback(Actions.SerialPorts.RegulateCallback), null);
        }

        void btn_rising_Click(object sender, RoutedEventArgs e)
        {
            if (!Common.PermissionManager.hasPower((int)Data.Enum.ControlPoints.RisingStart))
            {
                MessageBox.Show((string)TryFindResource("PermissionDenied"));
                return;
            }
            if (null != Actions.SerialPorts.Regulate && null != liftinfo)
                Actions.SerialPorts.Regulate.BeginInvoke(true,
                    liftinfo.LiftNum, liftinfo.LiftCOM, byte.Parse("8", NumberStyles.HexNumber), 0, new AsyncCallback(Actions.SerialPorts.RegulateCallback), null);
        }

        void EscalatorMonitoring_Loaded(object sender, RoutedEventArgs e)
        {
            Data.Model.ClientConfig r = Data.DAL.ClientConfig.Search();
            if (null != r)
            {
                if (null != r.EscalatorInput)
                {
                    List<string> tmp = r.EscalatorInput.Split(',').ToList();
                    if (tmp.Count < 3) { tmp.AddRange((new string[] { "", "", "" })); }
                }
            }
            Actions.LiftInfo.EscalatorMonitoring = doEscalatorMonitoring;
            PortMonitoring.MonitoringIDSign = this.LiftID;
            PortMonitoring.MonitoringSign = true;
        }

        bool isRising = false;
        bool isDescending = false;
        private void doEscalatorMonitoring(SerialDataEventArgs e)
        {
            if (Dispatcher.CheckAccess()) return;
            if (null == e || null == e.Data) return;
            if (this.liftnum != e.LiftNum.ToString()) return;
            Dispatcher.Invoke(new Action(() =>
            {
                this.status_standby.IsOn = Convert.ToString(e.Data.Data[41], 2).PadLeft(8, '0').Substring(6, 1) == "1" ? true : false;
                this.status_estop.IsOn = Convert.ToString(e.Data.Data[43], 2).PadLeft(8, '0').Substring(1, 1) == "1" ? true : false;
                this.status_fault.IsOn = Convert.ToString(e.Data.Data[41], 2).PadLeft(8, '0').Substring(2, 1) == "1" ? true : false;
                this.status_overhaul.IsOn = Convert.ToString(e.Data.Data[40], 2).PadLeft(8, '0').Substring(7, 1) == "1" ? true : false;
                this.gz_code.Content = e.Data.FaultCode;
                this.status_descending.IsOn = Convert.ToString(e.Data.Data[41], 2).PadLeft(8, '0').Substring(0, 1) == "1" ? true : false;
                this.status_rising.IsOn = Convert.ToString(e.Data.Data[41], 2).PadLeft(8, '0').Substring(1, 1) == "1" ? true : false;
                /*  this.status_standby.IsOn = e.Data.Standby;
                  this.status_estop.IsOn = e.Estop;
                  this.status_fault.IsOn = e.Data.FaultLamp;
                  this.status_overhaul.IsOn = e.Data.Overhaul;
                  this.gz_code.Content = e.Data.FaultCode;
                  this.status_descending.IsOn = e.Data.Descending;
                  this.status_rising.IsOn = e.Data.Rising;*/
            }));
        }

        void btn_close_Click(object sender, RoutedEventArgs e)
        {
            if (null != HideEventHandler)
                HideEventHandler.Invoke(this, null);
            // this.Close();
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            if (null != HideEventHandler)
                HideEventHandler.Invoke(this, null);
            // this.Close();
        }
        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private static void OnLiftIDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                Data.Model.LiftInfo model = Data.DAL.LiftInfo.Select((int)args.NewValue);
                if (null == model) { (sender as EscalatorMonitoring).lift_name.Content = (sender as EscalatorMonitoring).TryFindResource("NoID") as string; return; }
                InitModelInfo(sender as EscalatorMonitoring, model);
            }
        }

        private static void InitModelInfo(EscalatorMonitoring m, Data.Model.LiftInfo r)
        {
            m.liftinfo = r;
            m.liftnum = r.LiftNum.ToString();
            m.lift_name.Content = r.LiftNum + "--" + r.LiftSite;
        }

        private static void OnLiftDirectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if ((bool)args.NewValue)
                    (sender as EscalatorMonitoring).BeginStoryboard((Storyboard)(sender as EscalatorMonitoring).TryFindResource("UpRingStoryboard"));
                else
                    (sender as EscalatorMonitoring).BeginStoryboard((Storyboard)(sender as EscalatorMonitoring).TryFindResource("DownRingStoryboard"));
            }
        }

        void EscalatorMonitoring_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #region 以下注册属性
        /// <summary>
        /// 数据库内电梯信息ID
        /// </summary>
        public static readonly DependencyProperty LiftIDProperty =
            DependencyProperty.Register("LiftID", typeof(int), typeof(EscalatorMonitoring),
            new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnLiftIDChanged)));

        /// <summary>
        /// 数据库内电梯信息ID
        /// </summary>
        public int LiftID
        {
            get { return (int)GetValue(LiftIDProperty); }
            set { SetValue(LiftIDProperty, value); }
        }

        /// <summary>
        /// 电梯进行方向,true上行,false下行
        /// </summary>
        public static readonly DependencyProperty LiftDirectionProperty =
            DependencyProperty.Register("LiftDirection", typeof(bool), typeof(EscalatorMonitoring),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnLiftDirectionChanged)));

        /// <summary>
        /// 电梯进行方向,true上行,false下行
        /// </summary>
        public bool LiftDirection
        {
            get { return (bool)GetValue(LiftDirectionProperty); }
            set { SetValue(LiftDirectionProperty, value); }
        }
        #endregion

        private void btn_rising_Click_1(object sender, RoutedEventArgs e)
        {

        }
    }
}
